{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-end">
        <a href="{{ invoice }}" target="_blank" data-bs-toggle="tooltip" title="{{ button_invoice_print }}" class="btn btn-info{% if not order_id %} disabled{% endif %}"><i class="fas fa-print"></i></a> <a href="{{ shipping }}" target="_blank" data-bs-toggle="tooltip" title="{{ button_shipping_print }}" class="btn btn-info{% if not order_id %} disabled{% endif %}"><i class="fas fa-truck"></i></a> <a href="{{ back }}" data-bs-toggle="tooltip" title="{{ button_back }}" class="btn btn-light"><i class="fas fa-reply"></i></a></div>
      <h1>{{ heading_title }}</h1>
      <ol class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
          <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ol>
    </div>
  </div>
  <div class="container-fluid">
    <div class="card mb-3">
      <div class="card-header"><i class="fas fa-info-circle"></i> {{ text_form }}</div>
      <div class="card-body">

        <div id="collapse-order" class="overflow-hidden" style="max-height: 78px;">
          <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-xl-4">

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_invoice }}</strong>
                    <br/>
                    {{ invoice_no }}&nbsp;
                  </p>
                </div>
                {% if not invoice_no %}
                  <button type="button" id="button-invoice" data-oc-loading-text="{{ text_loading }}" data-bs-toggle="tooltip" title="{{ button_generate }}" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
                {% else %}
                  <button type="button" disabled="disabled" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
                {% endif %}
              </div>
            </div>
            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_customer }}</strong>
                    <br/>
                    {% if customer %}
                      <a href="{{ customer }}" target="_blank">{{ firstname }} {{ lastname }}</a>
                    {% else %}
                      {{ firstname }} {{ lastname }} &nbsp;
                    {% endif %}
                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-customer" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
              </div>
            </div>


            <div class="col">
              <div class="form-control-plaintext p-0 border rounded mb-3">
                <p class="lead p-2 mb-0"><strong>{{ text_date_added }}</strong>
                  <br/>
                  {{ date_added }}
                </p>
              </div>
            </div>

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_store }}</strong>
                    <br/>
                    <a href="{{ store_url }}" target="_blank">{{ store_name }}</a></p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-store" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
              </div>
            </div>

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_language }}</strong>
                    <br/>
                    {{ language_name }}
                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-language" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
              </div>
            </div>

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_currency }}</strong>
                    <br/>
                    {{ currency_title }}
                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-currency" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
              </div>
            </div>

            <div class="col">
              <div class="form-control p-0 border rounded mb-3">
                <p class="lead p-2 mb-0"><strong>{{ text_total }}</strong>
                  <br/>
                  {{ total }}
                </p>
              </div>
            </div>

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_payment_method }}</strong>
                    <br/>
                    {% if payment_method %}
                      {{ payment_method }}
                    {% else %}
                      &nbsp;
                    {% endif %}
                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-payment-method" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
              </div>
            </div>

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_shipping_method }}</strong>
                    <br/>
                    {% if shipping_method %}
                      {{ shipping_method }}
                    {% else %}
                      &nbsp;
                    {% endif %}
                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-shipping-method" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
              </div>
            </div>

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_coupon }}</strong>
                    <br/>
                    {{ coupon }}&nbsp;
                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-coupon" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
              </div>
            </div>

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_voucher }}</strong>
                    <br/>
                    {{ voucher }}&nbsp;
                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-voucher" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
              </div>
            </div>

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_reward }}</strong>
                    <br/>
                    {% if reward %}
                      {{ reward }}
                    {% else %}
                      &nbsp;
                    {% endif %}

                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-reward" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
                {% if customer and reward %}
                  {% if not reward_total %}
                    <button type="button" id="button-reward-add" data-oc-loading-text="{{ text_loading }}" data-bs-toggle="tooltip" title="{{ button_reward_add }}" class="btn btn-success"><i class="fas fa-plus-circle"></i></button>
                  {% else %}
                    <button type="button" id="button-reward-remove" data-oc-loading-text="{{ text_loading }}" data-bs-toggle="tooltip" title="{{ button_reward_remove }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button>
                  {% endif %}
                {% else %}
                  <button type="button" disabled="disabled" class="btn btn-success"><i class="fas fa-plus-circle"></i></button>
                {% endif %}
              </div>
            </div>

            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_affiliate }}</strong>
                    <br/>
                    {% if affiliate %}
                      <a href="{{ affiliate }}">{{ affiliate_firstname }} {{ affiliate_lastname }}</a>
                    {% else %}
                      &nbsp;
                    {% endif %}
                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-affiliate" class="btn btn-outline-primary"><i class="fas fa-cog"></i></button>
              </div>
            </div>
            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_commission }}</strong>
                    <br/>
                    {% if commission %}
                      {{ commission }}
                    {% else %}
                      &nbsp;
                    {% endif %}
                  </p>
                </div>
                {% if not commission_total %}
                  <button type="button" id="button-commission-add" data-oc-loading-text="{{ text_loading }}" data-bs-toggle="tooltip" title="{{ button_commission_add }}" class="btn btn-success"><i class="fas fa-plus-circle"></i></button>
                {% else %}
                  <button type="button" id="button-commission-remove" data-oc-loading-text="{{ text_loading }}" data-bs-toggle="tooltip" title="{{ button_commission_remove }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button>
                {% endif %}
              </div>
            </div>
          </div>
        </div>
        <button type="button" id="button-collapse" class="btn btn-light btn-lg w-100 mb-3">{{ text_more }} <i class="fas fa-angle-down"></i></button>
        <div class="row">
          <div class="col">
            <div class="input-group mb-3">
              <div class="form-control p-0 border rounded-start">
                <p class="lead p-2 mb-0"><strong>{{ text_payment_address }}</strong>
                  <br/>
                  {% if payment_address %}
                    {{ payment_address }}
                  {% else %}
                    &nbsp;
                    <br/>&nbsp;
                  {% endif %}
                </p>
              </div>
              <button type="button" data-bs-toggle="modal" data-bs-target="#modal-payment-address" class="btn btn-outline-primary btn-sm float-end"><i class="fas fa-cog"></i></button>
            </div>
          </div>
          <div class="col">
            <div class="input-group mb-3">
              <div class="form-control p-0 border rounded-start">
                <p class="lead p-2 mb-0"><strong>{{ text_shipping_address }}</strong>
                  <br/>
                  {% if shipping_address %}
                    {{ shipping_address }}
                  {% else %}
                    &nbsp;
                    <br/>&nbsp;
                  {% endif %}
                </p>
              </div>
              <button type="button" data-bs-toggle="modal" data-bs-target="#modal-shipping-address" class="btn btn-outline-primary btn-sm float-end"><i class="fas fa-cog"></i></button>
            </div>
          </div>
        </div>

        <table class="table table-bordered">
          <thead>
            <tr>
              <td class="text-start">{{ column_product }}</td>
              <td class="text-start">{{ column_model }}</td>
              <td class="text-end">{{ column_quantity }}</td>
              <td class="text-end">{{ column_price }}</td>
              <td class="text-end">{{ column_total }}</td>
              <td class="text-end" style="width: 1px;">{{ column_action }}</td>
            </tr>
          </thead>
          <tbody id="cart">
            {% if order_products or order_vouchers %}

              {% set product_row = 0 %}
              {% for order_product in order_products %}
                <tr>
                  <td class="text-start"><a href="{{ order_product.href }}">{{ order_product.name }}</a>
                    <br/>
                    <input type="hidden" name="product[{{ product_row }}][product_id]" value="{{ order_product.product_id }}"/>
                    {% for option in order_product.option %}
                      {% if option.type != 'file' %}
                        &nbsp;
                        <small> - {{ option.name }}: {{ option.value }}</small>
                      {% else %}
                        &nbsp;
                        <small> - {{ option.name }}: <a href="{{ option.href }}">{{ option.value }}</a></small>
                      {% endif %}
                      <br/>
                      {% if option.type == 'select' or option.type == 'radio' or option.type == 'image' %}
                        <input type="hidden" name="product[{{ product_row }}][option][{{ option.product_option_id }}]" value="{{ option.product_option_value_id }}" class="form-check-input"/>
                      {% endif %}
                      {% if option.type == 'checkbox' %}
                        <input type="hidden" name="product[{{ product_row }}][option][{{ option.product_option_id }}][]" value="{{ option.product_option_value_id }}" class="form-check-input"/>
                      {% endif %}
                      {% if option.type == 'text' or option.type == 'textarea' or option.type == 'file' or option.type == 'date' or option.type == 'datetime' or option.type == 'time' %}
                        <input type="hidden" name="product[{{ product_row }}][option][{{ option.product_option_id }}]" value="{{ option.value }}"/>
                      {% endif %}
                    {% endfor %}</td>
                  <td class="text-start">{{ order_product.model }}</td>
                  <td class="text-end">{{ order_product.quantity }}
                    <input type="hidden" name="product[{{ product_row }}][quantity]" value="{{ order_product.quantity }}"/></td>
                  <td class="text-end">{{ order_product.price }}</td>
                  <td class="text-end">{{ order_product.total }}</td>
                  <td class="text-end"><button type="button" data-bs-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button></td>
                </tr>
                {% set product_row = product_row + 1 %}
              {% endfor %}

              {% set voucher_row = 0 %}
              {% for order_voucher in order_vouchers %}
                <tr>
                  <td class="text-start"><a href="{{ order_voucher.href }}">{{ order_voucher.description }}</a> <input type="hidden" name="voucher[{{ voucher_row }}][voucher_id]" value="{{ order_voucher.voucher_id }}"/> <input type="hidden" name="voucher[{{ voucher_row }}][description]" value="{{ order_voucher.description }}"/> <input type="hidden" name="voucher[{{ voucher_row }}][code]" value="{{ order_voucher.code }}"/> <input type="hidden" name="voucher[{{ voucher_row }}][from_name]" value="{{ order_voucher.from_name }}"/> <input type="hidden" name="voucher[{{ voucher_row }}][from_email]" value="{{ order_voucher.from_email }}"/> <input type="hidden" name="voucher[{{ voucher_row }}][to_name]" value="{{ order_voucher.to_name }}"/> <input type="hidden" name="voucher[{{ voucher_row }}][to_email]" value="{{ order_voucher.to_email }}"/> <input type="hidden" name="voucher[{{ voucher_row }}][voucher_theme_id]" value="{{ order_voucher.voucher_theme_id }}"/>
                    <input type="hidden" name="voucher[{{ voucher_row }}][message]" value="{{ order_voucher.message }}"/> <input type="hidden" name="voucher[{{ voucher_row }}][amount]" value="{{ order_voucher.amount }}"/></td>
                  <td class="text-start"></td>
                  <td class="text-end">1</td>
                  <td class="text-end">{{ order_voucher.amount }}</td>
                  <td class="text-end">{{ order_voucher.amount }}</td>
                  <td class="text-end"><button type="button" data-bs-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button></td>
                </tr>
                {% set voucher_row = voucher_row + 1 %}
              {% endfor %}

            {% else %}

              <tr>
                <td class="text-center" colspan="6">{{ text_no_results }}</td>
              </tr>

            {% endif %}

            <tr>
              <td colspan="5"></td>
              <td class="text-end"><button type="button" data-bs-toggle="modal" data-bs-target="#modal-cart" class="btn btn-primary"><i class="fas fa-plus-circle"></i></button></td>
            </tr>

            {% for order_total in order_totals %}
              <tr>
                <td colspan="5" class="text-end">{{ order_total.title }}</td>
                <td class="text-end">{{ order_total.text }}</td>
              </tr>
            {% endfor %}

          </tbody>
        </table>

        {% if comment %}
          <div class="row">
            <div class="col">
              <div class="input-group mb-3">
                <div class="form-control-plaintext p-0 border rounded-start">
                  <p class="lead p-2 mb-0"><strong>{{ text_comment }}</strong>
                    <br/>
                    {{ comment }}
                  </p>
                </div>
                <button type="button" data-bs-toggle="modal" data-bs-target="#modal-comment" class="btn btn-outline-primary btn-sm float-end"><i class="fas fa-cog"></i></button>
              </div>
            </div>
          </div>
        {% endif %}

      </div>
    </div>
    <div class="card mb-3">
      <div class="card-header"><i class="fas fa-comment"></i> {{ text_history }}</div>
      <div class="card-body">
        <ul class="nav nav-tabs">
          <li class="nav-item"><a href="#tab-history" data-bs-toggle="tab" class="nav-link active">{{ tab_history }}</a></li>
          <li class="nav-item"><a href="#tab-additional" data-bs-toggle="tab" class="nav-link">{{ tab_additional }}</a></li>
          {% for tab in tabs %}
            <li class="nav-item"><a href="#tab-{{ tab.code }}" data-bs-toggle="tab" class="nav-link">{{ tab.title }}</a></li>
          {% endfor %}
        </ul>
        <div class="tab-content">
          <div id="tab-history" class="tab-pane active">
            <div id="history"></div>
            <br/>
            <fieldset>
              <legend>{{ text_history_add }}</legend>
              <div class="row mb-3">
                <label for="input-order-status" class="col-sm-2 col-form-label">{{ entry_order_status }}</label>
                <div class="col-sm-10">
                  <select name="order_status_id" id="input-order-status" class="form-select">
                    {% for order_statuses in order_statuses %}
                      <option value="{{ order_statuses.order_status_id }}"{% if order_statuses.order_status_id == order_status_id %} selected{% endif %}>{{ order_statuses.name }}</option>
                    {% endfor %}
                  </select>
                </div>
              </div>
              <div class="row mb-3">
                <label for="input-override" class="col-sm-2 col-form-label">{{ entry_override }}</label>
                <div class="col-sm-10">
                  <div class="btn-group">
                    <label class="btn btn-outline-secondary"><input type="radio" name="override" value="1" class="btn-check"/> {{ text_yes }}</label> <label class="btn btn-outline-secondary active"><input type="radio" name="override" value="0" class="btn-check" checked/> {{ text_no }}</label>
                  </div>
                  <div class="form-text text-muted">{{ help_override }}</div>
                </div>
              </div>
              <div class="row mb-3">
                <label for="input-notify" class="col-sm-2 col-form-label">{{ entry_notify }}</label>
                <div class="col-sm-10">
                  <div class="btn-group">
                    <label class="btn btn-outline-secondary"><input type="radio" name="notify" value="1" class="btn-check"/> {{ text_yes }}</label> <label class="btn btn-outline-secondary active"><input type="radio" name="notify" value="0" class="btn-check" checked/> {{ text_no }}</label>
                  </div>
                </div>
              </div>
              <div class="row mb-3">
                <label for="input-comment" class="col-sm-2 col-form-label">{{ entry_comment }}</label>
                <div class="col-sm-10">
                  <textarea name="comment" rows="8" placeholder="{{ entry_comment }}" id="input-comment" class="form-control"></textarea>
                </div>
              </div>
            </fieldset>
            <div class="text-end">
              <button type="button" id="button-history" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-plus-circle"></i> {{ button_history_add }}</button>
            </div>
          </div>
          <div id="tab-additional" class="tab-pane">
            <div class="table-responsive">
              <table class="table table-bordered">
                <thead>
                  <tr>
                    <td colspan="2">{{ text_browser }}</td>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>{{ text_ip }}</td>
                    <td>{{ ip }}</td>
                  </tr>
                  {% if forwarded_ip %}
                    <tr>
                      <td>{{ text_forwarded_ip }}</td>
                      <td>{{ forwarded_ip }}</td>
                    </tr>
                  {% endif %}
                  <tr>
                    <td>{{ text_user_agent }}</td>
                    <td>{{ user_agent }}</td>
                  </tr>
                  <tr>
                    <td>{{ text_accept_language }}</td>
                    <td>{{ accept_language }}</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
          {% for tab in tabs %}
            <div id="tab-{{ tab.code }}" class="tab-pane">{{ tab.content }}</div>
          {% endfor %}
        </div>
      </div>
    </div>
  </div>
</div>

<div id="modal-customer" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_customer_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-customer" data-oc-toggle="ajax">
          <div class="mb-3">
            <label for="input-customer" class="form-label">{{ entry_customer }}</label>
            <div class="input-group"><input type="text" name="customer" value="{{ firstname }} {{ lastname }}" placeholder="{{ entry_customer }}" id="input-customer" class="form-control"/> <input type="hidden" name="customer_id" value="{{ customer_id }}"/> <a href="{{ customer_add }}" target="_blank" data-bs-toggle="tooltip" title="{{ button_customer_add }}" class="btn btn-outline-secondary"><i class="fas fa-user-plus"></i></a></div>
          </div>
          <div class="mb-3">
            <label for="input-customer-group" class="form-label">{{ entry_customer_group }}</label> <select name="customer_group_id" id="input-customer-group" class="form-select">
              {% for customer_group in customer_groups %}
                <option value="{{ customer_group.customer_group_id }}"{% if customer_group.customer_group_id == customer_group_id %} selected{% endif %}>{{ customer_group.name }}</option>
              {% endfor %}
            </select>
          </div>
          <div class="mb-3 required">
            <label for="input-firstname" class="form-label">{{ entry_firstname }}</label> <input type="text" name="firstname" value="{{ firstname }}" placeholder="{{ entry_firstname }}" id="input-firstname" class="form-control"/>
            <div id="error-firstname" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-lastname" class="form-label">{{ entry_lastname }}</label> <input type="text" name="lastname" value="{{ lastname }}" placeholder="{{ entry_lastname }}" id="input-lastname" class="form-control"/>
            <div id="error-lastname" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-email" class="form-label">{{ entry_email }}</label>
            <div class="input-group">
              <input type="text" name="email" value="{{ email }}" placeholder="{{ entry_email }}" id="input-email" class="form-control"/> <a href="mailto:{{ email }}" class="btn btn-outline-secondary"><i class="fa fa-envelope"></i></a>
            </div>
            <div id="error-email" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-telephone" class="form-label">{{ entry_telephone }}</label> <input type="text" name="telephone" value="{{ telephone }}" placeholder="{{ entry_telephone }}" id="input-telephone" class="form-control"/>
            <div id="error-telephone" class="invalid-feedback"></div>
          </div>

          {% for custom_field in custom_fields %}
            {% if custom_field.location == 'account' %}

              {% if custom_field.type == 'select' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label> <select name="custom_field[{{ custom_field.custom_field_id }}]" id="input-custom-field-{{ custom_field.custom_field_id }}" class="form-select">
                    <option value="">{{ text_select }}</option>
                    {% for custom_field_value in custom_field.custom_field_value %}
                      <option value="{{ custom_field_value.custom_field_value_id }}"{% if account_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == account_custom_field[custom_field.custom_field_id] %} selected="selected"{% endif %}>{{ custom_field_value.name }}</option>
                    {% endfor %}
                  </select>
                  <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'radio' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label class="form-label">{{ custom_field.name }}</label>
                  <div id="input-custom-field-{{ custom_field.custom_field_id }}" class="form-control" style="height: 150px; overflow: auto;">
                    {% for custom_field_value in custom_field.custom_field_value %}
                      <div class="form-check">
                        <input type="radio" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" id="input-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-input"{% if account_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == account_custom_field[custom_field.custom_field_id] %} checked{% endif %}/> <label for="input-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-label">{{ custom_field_value.name }}</label>
                      </div>
                    {% endfor %}
                  </div>
                  <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'checkbox' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label class="form-label">{{ custom_field.name }}</label>
                  <div id="input-custom-field-{{ custom_field.custom_field_id }}" class="form-control" style="height: 150px; overflow: auto;">
                    {% for custom_field_value in custom_field.custom_field_value %}
                      <div class="form-check">
                        <input type="checkbox" name="custom_field[{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" id="input-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-input"{% if account_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id in account_custom_field[custom_field.custom_field_id] %} checked{% endif %}/> <label for="input-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-label">{{ custom_field_value.name }}</label>
                      </div>
                    {% endfor %}
                  </div>
                  <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'text' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label> <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ account_custom_field[custom_field.custom_field_id] ? account_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                  <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'textarea' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label> <textarea name="custom_field[{{ custom_field.custom_field_id }}]" rows="5" placeholder="{{ custom_field.name }}" id="input-custom-field-{{ custom_field.custom_field_id }}" class="form-control">{{ custom_field.value }}</textarea>
                  <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'file' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label class="form-label">{{ custom_field.name }}</label>
                  <button type="button" data-oc-toggle="upload" data-oc-url="index.php?route=tool/upload|upload&user_token={{ user_token }}" data-oc-target="#input-custom-field-{{ custom_field.custom_field_id }}" data-oc-loading-text="{{ text_loading }}" class="btn btn-light"><i class="fas fa-upload"></i> {{ button_upload }}</button>
                  <input type="hidden" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ account_custom_field[custom_field.custom_field_id] ? account_custom_field[custom_field.custom_field_id] }}" id="input-custom-field-{{ custom_field.custom_field_id }}"/>
                  <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'date' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label>
                  <div class="input-group date">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ account_custom_field[custom_field.custom_field_id] ? account_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                    <div class="input-group-text"><i class="fas fa-calendar"></i></div>
                    <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                  </div>
                </div>
              {% endif %}

              {% if custom_field.type == 'time' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label>
                  <div class="input-group time">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ account_custom_field[custom_field.custom_field_id] ? account_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                    <div class="input-group-text"><i class="fas fa-calendar"></i></div>
                    <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                  </div>
                </div>
              {% endif %}

              {% if custom_field.type == 'datetime' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-custom-field-{{ custom_field.custom_field_id }}" class="form-label">{{ custom_field.name }}</label>
                  <div class="input-group datetime">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ account_custom_field[custom_field.custom_field_id] ? account_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                    <div class="input-group-text"><i class="fas fa-calendar"></i></div>
                    <div id="error-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                  </div>
                </div>
              {% endif %}

            {% endif %}
          {% endfor %}
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-customer" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-store" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_store_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-store" data-oc-toggle="ajax">
          <select name="store_id" id="input-store" class="form-select">
            {% for store in stores %}
              <option value="{{ store.store_id }}"{% if store.store_id == store_id %} selected="selected"{% endif %}>{{ store.name }}</option>
            {% endfor %}
          </select>
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-store" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-language" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_language_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-language">
          <select name="language" id="input-language" class="form-select">
            {% for language in languages %}
              <option value="{{ language.code }}"{% if language.code == language_code %} selected="selected"{% endif %}>{{ language.name }}</option>
            {% endfor %}
          </select>
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-language" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-currency" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_currency_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-currency">
          <select name="currency" id="input-currency" class="form-select">
            {% for currency in currencies %}
              <option value="{{ currency.code }}"{% if currency.code == currency_code %} selected="selected"{% endif %}>{{ currency.title }}</option>
            {% endfor %}
          </select>
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-currency" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-shipping-method" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_shipping_method_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-shipping-method">
          <div class="input-group">
            <select name="shipping_method" id="input-shipping-method" class="form-select">
              <option value="">{{ text_select }}</option>
              {% if shipping_code %}
                <option value="{{ shipping_code }}" selected="selected">{{ shipping_method }}</option>
              {% endif %}
            </select>
            <button type="button" id="button-shipping-methods" data-oc-loading-text="{{ text_loading }}" class="btn btn-outline-primary"><i class="fas fa-sync"></i></button>
          </div>
        </form>
      </div>
      <div class="modal-footer text-start">
        <button type="button" id="button-shipping-method" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-payment-method" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_payment_method_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-payment-method">
          <div class="input-group">
            <select name="payment_method" id="input-payment-method" class="form-select">
              <option value="">{{ text_select }}</option>
              {% if payment_code %}
                <option value="{{ payment_code }}" selected="selected">{{ payment_method }}</option>
              {% endif %}
            </select>
            <button type="button" id="button-payment-methods" data-oc-loading-text="{{ text_loading }}" class="btn btn-outline-primary"><i class="fas fa-sync"></i></button>
          </div>
        </form>
      </div>
      <div class="modal-footer text-start">
        <button type="button" id="button-payment-method" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-coupon" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_coupon_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-coupon">
          <input type="text" name="coupon" value="{{ coupon }}" placeholder="{{ entry_coupon }}" id="input-coupon" class="form-control"/>
        </form>
      </div>
      <div class="modal-footer text-start">
        <button type="button" id="button-coupon" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-voucher" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_voucher_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-voucher">
          <input type="text" name="voucher" value="{{ voucher }}" placeholder="{{ entry_voucher }}" id="input-voucher" data-oc-loading-text="{{ text_loading }}" class="form-control"/>
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-voucher" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-reward" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_reward_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-reward">
          <input type="text" name="reward" value="{{ reward }}" placeholder="{{ entry_reward }}" id="input-reward" data-oc-loading-text="{{ text_loading }}" class="form-control"/>
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-reward" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-affiliate" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_affiliate_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-affiliate">
          <input type="text" name="affiliate" value="{% if affiliate_id %}{{ affiliate_firstname }} {{ affiliate_lastname }}{% endif %}" placeholder="{{ entry_affiliate }}" id="input-affiliate" class="form-control"/> <input type="hidden" name="affiliate_id" value="{{ affiliate_id }}"/>
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-affiliate" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-payment-address" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_payment_address_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-payment-address">

          <div class="mb-3">
            <label for="input-payment-address" class="form-label">{{ entry_address }}</label> <select name="payment_address" id="input-payment-address" class="form-select">
              <option value="0" selected="selected">{{ text_none }}</option>
              {% for address in addresses %}
                <option value="{{ address.address_id }}">{{ address.firstname }} {{ address.lastname }}, {{ address.address_1 }}, {{ address.city }}, {{ address.country }}</option>
              {% endfor %}
            </select>
          </div>

          <div class="mb-3 required">
            <label for="input-payment-firstname" class="form-label">{{ entry_firstname }}</label> <input type="text" name="firstname" value="{{ payment_firstname }}" placeholder="{{ entry_firstname }}" id="input-payment-firstname" class="form-control"/>
            <div id="error-payment-firstname" class="invalid-feedback"></div>
          </div>

          <div class="mb-3 required">
            <label for="input-payment-lastname" class="form-label">{{ entry_lastname }}</label> <input type="text" name="lastname" value="{{ payment_lastname }}" placeholder="{{ entry_lastname }}" id="input-payment-lastname" class="form-control"/>
            <div id="error-payment-lastname" class="invalid-feedback"></div>
          </div>

          <div class="mb-3">
            <label for="input-payment-company" class="form-label">{{ entry_company }}</label> <input type="text" name="company" value="{{ payment_company }}" placeholder="{{ entry_company }}" id="input-payment-company" class="form-control"/>
          </div>

          <div class="mb-3 required">
            <label for="input-payment-address-1" class="form-label">{{ entry_address_1 }}</label> <input type="text" name="address_1" value="{{ payment_address_1 }}" placeholder="{{ entry_address_1 }}" id="input-payment-address-1" class="form-control"/>
            <div id="error-payment-address-1" class="invalid-feedback"></div>
          </div>

          <div class="mb-3">
            <label for="input-payment-address-2" class="form-label">{{ entry_address_2 }}</label> <input type="text" name="address_2" value="{{ payment_address_2 }}" placeholder="{{ entry_address_2 }}" id="input-payment-address-2" class="form-control"/>
          </div>

          <div class="mb-3 required">
            <label for="input-payment-city" class="form-label">{{ entry_city }}</label> <input type="text" name="city" value="{{ payment_city }}" placeholder="{{ entry_city }}" id="input-payment-city" class="form-control"/>
            <div id="error-payment-city" class="invalid-feedback"></div>
          </div>

          <div class="mb-3 required">
            <label for="input-payment-postcode" class="form-label">{{ entry_postcode }}</label> <input type="text" name="postcode" value="{{ payment_postcode }}" placeholder="{{ entry_postcode }}" id="input-payment-postcode" class="form-control"/>
            <div id="error-payment-postcode" class="invalid-feedback"></div>
          </div>

          <div class="mb-3 required">
            <label for="input-payment-country" class="form-label">{{ entry_country }}</label> <select name="country_id" id="input-payment-country" class="form-select">
              <option value="">{{ text_select }}</option>
              {% for country in countries %}
                <option value="{{ country.country_id }}"{% if country.country_id == payment_country_id %} selected="selected"{% endif %}>{{ country.name }}</option>
              {% endfor %}
            </select>
            <div id="error-payment-country" class="invalid-feedback"></div>
          </div>

          <div class="mb-3 required">
            <label for="input-payment-zone" class="col-form-label">{{ entry_zone }}</label> <select name="zone_id" id="input-payment-zone" class="form-select"></select>
            <div id="error-payment-zone" class="invalid-feedback"></div>
          </div>

          {% for custom_field in custom_fields %}
            {% if custom_field.location == 'address' %}

              {% if custom_field.type == 'select' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label> <select name="custom_field[{{ custom_field.custom_field_id }}]" id="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="form-select">
                    <option value="">{{ text_select }}</option>
                    {% for custom_field_value in custom_field.custom_field_value %}
                      <option value="{{ custom_field_value.custom_field_value_id }}"{% if payment_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == payment_custom_field[custom_field.custom_field_id] %} selected="selected"{% endif %}>{{ custom_field_value.name }}</option>
                    {% endfor %}
                  </select>
                  <div id="error-payment-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'radio' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label class="col-form-label">{{ custom_field.name }}</label>
                  <div id="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="form-control" style="height: 150px; overflow: auto;">
                    {% for custom_field_value in custom_field.custom_field_value %}
                      <div class="form-check">
                        <input type="radio" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" id="input-payment-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-input"{% if payment_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == payment_custom_field[custom_field.custom_field_id] %} checked{% endif %}/> <label for="input-payment-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-label">{{ custom_field_value.name }}</label>
                      </div>
                    {% endfor %}
                  </div>
                  <div id="error-payment-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'checkbox' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label class="col-form-label">{{ custom_field.name }}</label>
                  <div id="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="form-control" style="height: 150px; overflow: auto;">
                    {% for custom_field_value in custom_field.custom_field_value %}
                      <div class="form-check">
                        <input type="checkbox" name="custom_field[{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" id="input-payment-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-input"{% if payment_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id in payment_custom_field[custom_field.custom_field_id] %} checked{% endif %}/> <label for="input-payment-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-label">{{ custom_field_value.name }}</label>
                      </div>
                    {% endfor %}
                  </div>
                  <div id="error-payment-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'text' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label> <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ payment_custom_field[custom_field.custom_field_id] ? payment_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                  <div id="error-payment-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'textarea' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label> <textarea name="custom_field[{{ custom_field.custom_field_id }}]" rows="5" placeholder="{{ custom_field.name }}" id="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="form-control">{{ payment_custom_field[custom_field.custom_field_id] ? payment_custom_field[custom_field.custom_field_id] : custom_field.value }}</textarea>
                  <div id="error-payment-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'file' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label class="col-form-label">{{ custom_field.name }}</label>
                  <button type="button" data-oc-toggle="upload" data-oc-url="index.php?route=tool/upload|upload&user_token={{ user_token }}" data-oc-target="#input-payment-custom-field-{{ custom_field.custom_field_id }}" data-oc-loading-text="{{ text_loading }}" class="btn btn-light"><i class="fas fa-upload"></i> {{ button_upload }}</button>
                  <input type="hidden" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ payment_custom_field[custom_field.custom_field_id] ? payment_custom_field[custom_field.custom_field_id] }}" id="input-payment-custom-field-{{ custom_field.custom_field_id }}"/>
                  <div id="error-payment-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'date' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label>
                  <div class="input-group date">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ payment_custom_field[custom_field.custom_field_id] ? payment_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                    <div class="input-group-text"><i class="fas fa-calendar"></i></div>
                  </div>
                  <div id="error-payment-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'time' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label>
                  <div class="input-group time">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ payment_custom_field[custom_field.custom_field_id] ? payment_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                    <div class="input-group-text"><i class="fas fa-calendar"></i></div>
                  </div>
                  <div id="error-payment-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'datetime' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label>
                  <div class="input-group datetime">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ payment_custom_field[custom_field.custom_field_id] ? payment_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-payment-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                    <div class="input-group-text"><i class="fas fa-calendar"></i></div>
                  </div>
                  <div id="error-payment-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

            {% endif %}

          {% endfor %}
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-payment-address" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-shipping-address" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_shipping_address_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-shipping-address">

          <div class="mb-3">
            <label for="input-shipping-address" class="form-label">{{ entry_address }}</label> <select name="shipping_address" id="input-shipping-address" class="form-select">
              <option value="0" selected="selected">{{ text_none }}</option>
              {% for address in addresses %}
                <option value="{{ address.address_id }}">{{ address.firstname }} {{ address.lastname }}, {{ address.address_1 }}, {{ address.city }}, {{ address.country }}</option>
              {% endfor %}
            </select>
          </div>

          <div class="mb-3 required">
            <label for="input-shipping-firstname" class="form-label">{{ entry_firstname }}</label> <input type="text" name="firstname" value="{{ shipping_firstname }}" placeholder="{{ entry_firstname }}" id="input-shipping-firstname" class="form-control"/>
            <div id="error-shipping-firstname" class="invalid-feedback"></div>
          </div>

          <div class="mb-3 required">
            <label for="input-shipping-lastname" class="form-label">{{ entry_lastname }}</label> <input type="text" name="lastname" value="{{ shipping_lastname }}" placeholder="{{ entry_lastname }}" id="input-shipping-lastname" class="form-control"/>
            <div id="error-shipping-lastname" class="invalid-feedback"></div>
          </div>

          <div class="mb-3">
            <label for="input-shipping-company" class="form-label">{{ entry_company }}</label> <input type="text" name="company" value="{{ shipping_company }}" placeholder="{{ entry_company }}" id="input-shipping-company" class="form-control"/>
          </div>

          <div class="mb-3 required">
            <label for="input-shipping-address-1" class="form-label">{{ entry_address_1 }}</label> <input type="text" name="address_1" value="{{ shipping_address_1 }}" placeholder="{{ entry_address_1 }}" id="input-shipping-address-1" class="form-control"/>
            <div id="error-shipping-address-1" class="invalid-feedback"></div>
          </div>

          <div class="mb-3">
            <label for="input-shipping-address-2" class="form-label">{{ entry_address_2 }}</label> <input type="text" name="address_2" value="{{ shipping_address_2 }}" placeholder="{{ entry_address_2 }}" id="input-shipping-address-2" class="form-control"/>
          </div>
          <div class="mb-3 required">
            <label for="input-shipping-city" class="form-label">{{ entry_city }}</label> <input type="text" name="city" value="{{ shipping_city }}" placeholder="{{ entry_city }}" id="input-shipping-city" class="form-control"/>
            <div id="error-shipping-city" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-shipping-postcode" class="form-label">{{ entry_postcode }}</label> <input type="text" name="postcode" value="{{ shipping_postcode }}" placeholder="{{ entry_postcode }}" id="input-shipping-postcode" class="form-control"/>
            <div id="error-shipping-postcode" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-shipping-country" class="form-label">{{ entry_country }}</label> <select name="country_id" id="input-shipping-country" class="form-select">
              <option value="">{{ text_select }}</option>
              {% for country in countries %}
                <option value="{{ country.country_id }}"{% if country.country_id == shipping_country_id %} selected="selected"{% endif %}>{{ country.name }}</option>
              {% endfor %}
            </select>
            <div id="error-shipping-country" class="invalid-feedback"></div>
          </div>
          <div class="mb-3 required">
            <label for="input-shipping-zone" class="col-form-label">{{ entry_zone }}</label> <select name="zone_id" id="input-shipping-zone" class="form-select"></select>
            <div id="error-shipping-zone" class="invalid-feedback"></div>
          </div>

          {% for custom_field in custom_fields %}
            {% if custom_field.location == 'address' %}

              {% if custom_field.type == 'select' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label> <select name="custom_field[{{ custom_field.custom_field_id }}]" id="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="form-select">
                    <option value="">{{ text_select }}</option>
                    {% for custom_field_value in custom_field.custom_field_value %}
                      <option value="{{ custom_field_value.custom_field_value_id }}"{% if shipping_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == shipping_custom_field[custom_field.custom_field_id] %} selected="selected"{% endif %}>{{ custom_field_value.name }}</option>
                    {% endfor %}
                  </select>
                  <div id="error-shipping-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'radio' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label class="col-form-label">{{ custom_field.name }}</label>
                  <div id="input-shipping-custom-field-{{ custom_field.custom_field_id }}">
                    {% for custom_field_value in custom_field.custom_field_value %}
                      <div class="form-check">
                        <input type="radio" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" id="input-shipping-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-input"{% if shipping_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == shipping_custom_field[custom_field.custom_field_id] %} checked{% endif %}/> <label for="input-shipping-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-label">{{ custom_field_value.name }}</label>
                      </div>
                    {% endfor %}
                  </div>
                  <div id="error-shipping-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'checkbox' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label class="col-form-label">{{ custom_field.name }}</label>
                  <div id="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="form-control" style="height: 150px; overflow: auto;">
                    {% for custom_field_value in custom_field.custom_field_value %}
                      <div class="form-check">
                        <input type="checkbox" name="custom_field[{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" id="input-shipping-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-input"{% if shipping_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id in shipping_custom_field[custom_field.custom_field_id] %} checked{% endif %}/> <label for="input-shipping-custom-value-{{ custom_field_value.custom_field_value_id }}" class="form-check-label">{{ custom_field_value.name }}</label>
                      </div>
                    {% endfor %}
                  </div>
                  <div id="error-shipping-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'text' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label> <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ shipping_custom_field[custom_field.custom_field_id] ? shipping_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                  <div id="error-shipping-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'textarea' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label> <textarea name="custom_field[{{ custom_field.custom_field_id }}]" rows="5" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field-{{ custom_field.custom_field_id }}" placeholder="{{ custom_field.name }}" class="form-control">{{ shipping_custom_field[custom_field.custom_field_id] ? shipping_custom_field[custom_field.custom_field_id] : custom_field.value }}</textarea>
                  <div id="error-shipping-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'file' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label class="col-form-label">{{ custom_field.name }}</label>
                  <button type="button" data-oc-toggle="upload" data-oc-url="index.php?route=tool/upload|upload&user_token={{ user_token }}" data-oc-target="#input-shipping-custom-field-{{ custom_field.custom_field_id }}" data-oc-loading-text="{{ text_loading }}" class="btn btn-light"><i class="fas fa-upload"></i> {{ button_upload }}</button>
                  <input type="hidden" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ shipping_custom_field[custom_field.custom_field_id] ? shipping_custom_field[custom_field.custom_field_id] }}" id="input-shipping-custom-field-{{ custom_field.custom_field_id }}"/>
                  <div id="error-shipping-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'date' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label>
                  <div class="input-group date">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ shipping_custom_field[custom_field.custom_field_id] ? shipping_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                    <div class="input-group-text"><i class="fas fa-calendar"></i></div>
                  </div>
                  <div id="error-shipping-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'time' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label>
                  <div class="input-group time">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ shipping_custom_field[custom_field.custom_field_id] ? shipping_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                    <div class="input-group-text"><i class="fas fa-calendar"></i></div>
                  </div>
                  <div id="error-shipping-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

              {% if custom_field.type == 'datetime' %}
                <div class="mb-3 custom-field custom-field-{{ custom_field.custom_field_id }}">
                  <label for="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="col-form-label">{{ custom_field.name }}</label>
                  <div class="input-group datetime">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ shipping_custom_field[custom_field.custom_field_id] ? shipping_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-shipping-custom-field-{{ custom_field.custom_field_id }}" class="form-control"/>
                    <div class="input-group-text"><i class="fas fa-calendar"></i></div>
                  </div>
                  <div id="error-shipping-custom-field-{{ custom_field.custom_field_id }}" class="invalid-feedback"></div>
                </div>
              {% endif %}

            {% endif %}
          {% endfor %}
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-shipping-address" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<div id="modal-cart" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_cart_add }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <ul class="nav nav-tabs">
          <li class="nav-item"><a href="#tab-product" data-bs-toggle="tab" class="nav-link active">{{ tab_product }}</a></li>
          <li class="nav-item"><a href="#tab-voucher" data-bs-toggle="tab" class="nav-link">{{ tab_voucher }}</a></li>
        </ul>
        <div class="tab-content">

          <div id="tab-product" class="tab-pane active">
            <form id="form-cart-product">
              <fieldset>
                <legend>{{ text_product_add }}</legend>
                <div class="mb-3">
                  <label for="input-product" class="form-label">{{ entry_product }}</label> <input type="text" name="product" value="" placeholder="{{ entry_product }}" id="input-product" class="form-control"/> <input type="hidden" name="product_id" value=""/>
                </div>
                <div class="mb-3">
                  <label for="input-quantity" class="form-label">{{ entry_quantity }}</label> <input type="text" name="quantity" placeholder="{{ entry_quantity }}" value="1" id="input-quantity" class="form-control"/>
                </div>
              </fieldset>
              <div id="option"></div>
              <div class="text-end">
                <button type="button" id="button-product-add" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
              </div>
            </form>
          </div>

          <div id="tab-voucher" class="tab-pane">

            <form id="form-cart-voucher">
              <fieldset>
                <legend>{{ text_voucher_add }}</legend>
                <div class="mb-3 required">
                  <label for="input-to-name" class="col-form-label">{{ entry_to_name }}</label> <input type="text" name="to_name" value="" placeholder="{{ entry_to_name }}" id="input-to-name" class="form-control"/>
                </div>
                <div class="mb-3 required">
                  <label for="input-to-email" class="col-form-label">{{ entry_to_email }}</label> <input type="text" name="to_email" value="" placeholder="{{ entry_to_email }}" id="input-to-email" class="form-control"/>
                </div>
                <div class="mb-3 required">
                  <label for="input-from-name" class="col-form-label">{{ entry_from_name }}</label> <input type="text" name="from_name" value="" placeholder="{{ entry_from_name }}" id="input-from-name" class="form-control"/>
                </div>
                <div class="mb-3 required">
                  <label for="input-from-email" class="col-form-label">{{ entry_from_email }}</label> <input type="text" name="from_email" value="" placeholder="{{ entry_from_email }}" id="input-from-email" class="form-control"/>
                </div>
                <div class="mb-3 required">
                  <label for="input-theme" class="col-form-label">{{ entry_theme }}</label> <select name="voucher_theme_id" id="input-theme" class="form-select">
                    {% for voucher_theme in voucher_themes %}
                      <option value="{{ voucher_theme.voucher_theme_id }}">{{ voucher_theme.name }}</option>
                    {% endfor %}
                  </select>
                </div>
                <div class="mb-3">
                  <label for="input-message" class="form-label">{{ entry_message }}</label> <textarea name="message" rows="5" placeholder="{{ entry_message }}" id="input-message" class="form-control"></textarea>
                </div>
                <div class="mb-3 required">
                  <label for="input-amount" class="form-label">{{ entry_amount }}</label> <input type="text" name="amount" value="{{ voucher_min }}" placeholder="{{ entry_amount }}" id="input-amount" class="form-control"/>
                </div>
              </fieldset>
              <div class="text-end">
                <button type="button" id="button-voucher-add" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
              </div>
            </form>

          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div id="modal-comment" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ text_comment_edit }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <form id="form-comment">
          <textarea name="comment" rows="5" placeholder="{{ entry_comment }}" id="input-comment" class="form-control">{{ comment }}</textarea>
        </form>
      </div>
      <div class="modal-footer text-end">
        <button type="button" id="button-comment" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-save"></i> {{ button_save }}</button>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript"><!--
$(document).on('click', '#button-collapse', function() {
    var element = this;

    if ($('#collapse-order').css('max-height') == '78px') {
        $('#button-collapse').html('{{ text_less }} <i class="fas fa-angle-up"></i>');

        $('#collapse-order').animate({'max-height': $('#container').height() + 'px'});
    } else {
        $('#button-collapse').html('{{ text_more }} <i class="fas fa-angle-down"></i>');

        $('#collapse-order').animate({'max-height': '78px'});
    }
});

$(document).on('click', '#button-invoice', function() {
    $.ajax({
        url: 'index.php?route=sale/order|createinvoiceno&user_token={{ user_token }}&order_id={{ order_id }}',
        dataType: 'json',
        beforeSend: function() {
            $('#button-invoice').button('loading');
        },
        complete: function() {
            $('#button-invoice').button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['invoice_no']) {
                $('#invoice').html(json['invoice_no']);

                $('#button-invoice').replaceWith('<button disabled="disabled" class="btn btn-success"><i class="fas fa-cog"></i></button>');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#modal-store #button-store').on('click', function() {
    // Refresh products, vouchers and totals
    $('#button-refresh').trigger('click');
});

// Customer
$('#input-customer').autocomplete({
    'source': function(request, response) {
        $.ajax({
            url: 'index.php?route=customer/customer|autocomplete&user_token={{ user_token }}&filter_name=' + encodeURIComponent(request),
            dataType: 'json',
            success: function(json) {
                json.unshift({
                    customer_id: '0',
                    customer_group_id: '{{ customer_group_id }}',
                    name: '{{ text_none }}',
                    customer_group: '',
                    firstname: '',
                    lastname: '',
                    email: '',
                    telephone: '',
                    custom_field: [],
                    address: []
                });

                response($.map(json, function(item) {
                    return {
                        category: item['customer_group'],
                        label: item['name'],
                        value: item['customer_id'],
                        customer_group_id: item['customer_group_id'],
                        firstname: item['firstname'],
                        lastname: item['lastname'],
                        email: item['email'],
                        telephone: item['telephone'],
                        custom_field: item['custom_field'],
                        address: item['address']
                    }
                }));
            }
        });
    },
    'select': function(item) {
        // Reset all custom fields
        $('#modal-customer input[type=\'text\'], #modal-customer textarea').not('#modal-customer input[name=\'customer\'], #modal-customer input[name=\'customer_id\']').val('');
        $('#modal-customer select option').removeAttr('selected');
        $('#modal-customer input[type=\'checkbox\'], #modal-customer input[type=\'radio\']').removeAttr('checked');

        $('#modal-customer #input-customer').val(item['label']);
        $('#modal-customer #input-customer-id').val(item['value']);
        $('#modal-customer #input-customer-group').val(item['customer_group_id']);
        $('#modal-customer #input-firstname').val(item['firstname']);
        $('#modal-customer #input-lastname').val(item['lastname']);
        $('#modal-customer #input-email').val(item['email']);
        $('#modal-customer #input-telephone').val(item['telephone']);

        for (i in item.custom_field) {
            $('#modal-customer select[name=\'custom_field[' + i + ']\']').val(item.custom_field[i]);
            $('#modal-customer textarea[name=\'custom_field[' + i + ']\']').val(item.custom_field[i]);
            $('#modal-customer input[name^=\'custom_field[' + i + ']\'][type=\'text\']').val(item.custom_field[i]);
            $('#modal-customer input[name^=\'custom_field[' + i + ']\'][type=\'hidden\']').val(item.custom_field[i]);
            $('#modal-customer input[name^=\'custom_field[' + i + ']\'][type=\'radio\'][value=\'' + item.custom_field[i] + '\']').prop('checked', true);

            if (item.custom_field[i] instanceof Array) {
                for (j = 0; j < item.custom_field[i].length; j++) {
                    $('#modal-customer input[name^=\'custom_field[' + i + ']\'][type=\'checkbox\'][value=\'' + item.custom_field[i][j] + '\']').prop('checked', true);
                }
            }
        }

        $('#modal-customer select[name=\'customer_group_id\']').trigger('change');

        html = '<option value="0">{{ text_none }}</option>';

        for (i in item['address']) {
            html += '<option value="' + item['address'][i]['address_id'] + '">' + item['address'][i]['firstname'] + ' ' + item['address'][i]['lastname'] + ', ' + item['address'][i]['address_1'] + ', ' + item['address'][i]['city'] + ', ' + item['address'][i]['country'] + '</option>';
        }

        $('#modal-payment-address select[name=\'payment_address\']').html(html);
        $('#modal-shipping-address select[name=\'shipping_address\']').html(html);
    }
});

// Custom Fields
$('#modal-customer select[name=\'customer_group_id\']').on('change', function() {
    $.ajax({
        url: 'index.php?route=customer/customer|customfield&user_token={{ user_token }}&customer_group_id=' + this.value,
        dataType: 'json',
        success: function(json) {
            $('.custom-field').hide();
            $('.custom-field').removeClass('required');

            for (i = 0; i < json.length; i++) {
                custom_field = json[i];

                $('.custom-field-' + custom_field['custom_field_id']).show();

                if (custom_field['required']) {
                    $('.custom-field-' + custom_field['custom_field_id']).addClass('required');
                }
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#modal-customer select[name=\'customer_group_id\']').trigger('change');

// Customer
$('#modal-customer #button-customer').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val() + '&action=customer|load',
        type: 'post',
        dataType: 'html',
        data: $('#modal-customer input[type=\'text\'], #modal-customer input[type=\'hidden\'], #modal-customer input[type=\'radio\']:checked, #modal-customer input[type=\'checkbox\']:checked, #modal-customer select, #modal-customer textarea'),
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible, .invalid-feedback').remove();
            $('.is-invalid').removeClass('is-invalid');

            // Check for errors
            if (json['error']) {
                if (json['error']['warning']) {
                    $('#modal-customer .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['warning'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                }

                console.log(json['error']);

                for (key in json['error']) {
                    oc.error(key, json['error'][key]);
                }
            }

            if (json['success']) {
                $('#modal-customer .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Language
$('#button-language').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|language&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        dataType: 'json',
        data: 'language=' + $('select[name=\'language\'] option:selected').val(),
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#modal-language .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#modal-language .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Currency
$('#button-currency').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|currency&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        dataType: 'json',
        data: 'currency=' + $('select[name=\'currency\'] option:selected').val(),
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#modal-currency .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#modal-currency .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Shipping Method
$('#modal-shipping-method #button-shipping-methods').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|shippingmethods&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            if (json['error']) {
                $('#modal-shipping-method .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            } else {
                // Shipping Methods
                html = '<option value="">{{ text_select }}</option>';

                if (json['shipping_methods']) {
                    for (i in json['shipping_methods']) {
                        html += '<optgroup label="' + json['shipping_methods'][i]['title'] + '">';

                        if (!json['shipping_methods'][i]['error']) {
                            for (j in json['shipping_methods'][i]['quote']) {
                                if (json['shipping_methods'][i]['quote'][j]['code'] == $('#modal-shipping-method select[name=\'shipping_method\'] option:selected').val()) {
                                    html += '<option value="' + json['shipping_methods'][i]['quote'][j]['code'] + '" selected="selected">' + json['shipping_methods'][i]['quote'][j]['title'] + ' - ' + json['shipping_methods'][i]['quote'][j]['text'] + '</option>';
                                } else {
                                    html += '<option value="' + json['shipping_methods'][i]['quote'][j]['code'] + '">' + json['shipping_methods'][i]['quote'][j]['title'] + ' - ' + json['shipping_methods'][i]['quote'][j]['text'] + '</option>';
                                }
                            }
                        } else {
                            html += '<option value="" style="color: #ff0000;" disabled="disabled">' + json['shipping_methods'][i]['error'] + '</option>';
                        }

                        html += '</optgroup>';
                    }
                }

                $('select[name=\'shipping_method\']').html(html);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#modal-shipping-method #button-shipping-method').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|shippingmethod&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: 'shipping_method=' + $('select[name=\'shipping_method\'] option:selected').val(),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#modal-shipping-method .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#modal-shipping-method .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Payment Method
$('#modal-payment-method #button-payment-methods').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|paymentmethods&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            if (json['error']) {
                $('#collapse-payment .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            } else {
                html = '<option value="">{{ text_select }}</option>';

                if (json['payment_methods']) {
                    for (i in json['payment_methods']) {
                        if (json['payment_methods'][i]['code'] == $('select[name=\'payment_method\'] option:selected').val()) {
                            html += '<option value="' + json['payment_methods'][i]['code'] + '" selected="selected">' + json['payment_methods'][i]['title'] + '</option>';
                        } else {
                            html += '<option value="' + json['payment_methods'][i]['code'] + '">' + json['payment_methods'][i]['title'] + '</option>';
                        }
                    }
                }

                $('select[name=\'payment_method\']').html(html);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#modal-payment-method #button-payment-method').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|paymentmethod&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: 'payment_method=' + $('select[name=\'payment_method\'] option:selected').val(),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('lo ading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#modal-payment-method .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#modal-payment-method .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Coupon
$('#modal-coupon #button-coupon').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|coupon&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: 'coupon=' + $('input[name=\'coupon\']').val(),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#modal-coupon .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#modal-coupon .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Voucher
$('#modal-voucher #button-voucher').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|voucher&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: 'voucher=' + $('input[name=\'voucher\']').val(),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#modal-voucher .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#modal-voucher .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Reward
$('#modal-reward #button-reward').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|reward&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: 'reward=' + $('input[name=\'reward\']').val(),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible, .invalid-tooltip').remove();
            $('.is-invalid').removeClass('is-invalid');

            if (json['error']) {
                $('#modal-reward .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Highlight any found errors
                $('input[name=\'reward\']').addClass('is-invalid');
            }

            if (json['success']) {
                $('#modal-reward .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Merged rewards and commission together
$(document).on('click', '#button-reward-add, #button-reward-remove, #button-commission-add, #button-commission-remove', function(e) {
    var element = this;

    var part = $(this).attr('id').split('-');

    $.ajax({
        url: 'index.php?route=sale/order|call&user_token={{ user_token }}&action=' + part[2] + part[1] + '&order_id=' + $('input[name=\'order_id\']').val(),
        type: 'post',
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                if (part[1] == 'reward') {
                    if (part[2] == 'add') {
                        $(element).replaceWith('<button type="button" id="button-reward-remove" data-bs-toggle="tooltip" title="{{ button_reward_remove }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button>');
                    } else {
                        $(element).replaceWith('<button type="button" id="button-reward-add" data-bs-toggle="tooltip" title="{{ button_reward_add }}" class="btn btn-success"><i class="fas fa-plus-circle"></i></button>');
                    }
                }

                if (part[1] == 'commission') {
                    if (part[2] == 'add') {
                        $(element).replaceWith('<button type="button" id="button-commission-remove" data-bs-toggle="tooltip" title="{{ button_commission_remove }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button>');
                    } else {
                        $(element).replaceWith('<button type="button" id="button-commission-add" data-bs-toggle="tooltip" title="{{ button_commission_add }}" class="btn btn-success"><i class="fas fa-plus-circle"></i></button>');
                    }
                }
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Affiliate
$('input[name=\'affiliate\']').autocomplete({
    'source': function(request, response) {
        $.ajax({
            url: 'index.php?route=customer/customer|autocomplete&user_token={{ user_token }}&filter_affiliate=1&filter_name=' + encodeURIComponent(request),
            dataType: 'json',
            success: function(json) {
                json.unshift({
                    customer_id: 0,
                    name: '{{ text_none }}'
                });

                response($.map(json, function(item) {
                    return {
                        label: item['name'],
                        value: item['customer_id']
                    }
                }));
            }
        });
    },
    'select': function(item) {
        $('input[name=\'affiliate\']').val(item['label']);
        $('input[name=\'affiliate_id\']').val(item['value']);
    }
});

// Payment Address
$('#modal-payment-address select[name=\'payment_address\']').on('change', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=customer/customer|address&user_token={{ user_token }}&address_id=' + this.value,
        dataType: 'json',
        beforeSend: function() {
            $(element).prop('disabled', true);
        },
        complete: function() {
            $(element).prop('disabled', false);
        },
        success: function(json) {
            // Reset all fields
            $('#modal-payment-address input[type=\'text\'], #modal-payment-address input[type=\'text\'], #modal-payment-address textarea').val('');
            $('#modal-payment-address select option').not('#modal-payment-address select[name=\'payment_address\']').removeAttr('selected');
            $('#modal-payment-address input[type=\'checkbox\'], #modal-payment-address input[type=\'radio\']').removeAttr('checked');

            $('#modal-payment-address input[name=\'firstname\']').val(json['firstname']);
            $('#modal-payment-address input[name=\'lastname\']').val(json['lastname']);
            $('#modal-payment-address input[name=\'company\']').val(json['company']);
            $('#modal-payment-address input[name=\'address_1\']').val(json['address_1']);
            $('#modal-payment-address input[name=\'address_2\']').val(json['address_2']);
            $('#modal-payment-address input[name=\'city\']').val(json['city']);
            $('#modal-payment-address input[name=\'postcode\']').val(json['postcode']);
            $('#modal-payment-address select[name=\'country_id\']').val(json['country_id']);

            payment_zone_id = json['zone_id'];

            for (i in json['custom_field']) {
                $('#modal-payment-address select[name=\'custom_field[' + i + ']\']').val(json['custom_field'][i]);
                $('#modal-payment-address textarea[name=\'custom_field[' + i + ']\']').val(json['custom_field'][i]);
                $('#modal-payment-address input[name^=\'custom_field[' + i + ']\'][type=\'text\']').val(json['custom_field'][i]);
                $('#modal-payment-address input[name^=\'custom_field[' + i + ']\'][type=\'hidden\']').val(json['custom_field'][i]);
                $('#modal-payment-address input[name^=\'custom_field[' + i + ']\'][type=\'radio\'][value=\'' + json['custom_field'][i] + '\']').prop('checked', true);
                $('#modal-payment-address input[name^=\'custom_field[' + i + ']\'][type=\'checkbox\'][value=\'' + json['custom_field'][i] + '\']').prop('checked', true);

                if (json['custom_field'][i] instanceof Array) {
                    for (j = 0; j < json['custom_field'][i].length; j++) {
                        $('#modal-payment-address input[name^=\'custom_field[' + i + ']\'][type=\'checkbox\'][value=\'' + json['custom_field'][i][j] + '\']').prop('checked', true);
                    }
                }
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    }).done(function() {
        $('#modal-payment-address select[name=\'country_id\']').trigger('change');
    });
});

var payment_zone_id = '{{ payment_zone_id }}';

$('#modal-payment-address select[name=\'country_id\']').on('change', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=localisation/country|country&user_token={{ user_token }}&country_id=' + this.value,
        dataType: 'json',
        beforeSend: function() {
            $(element).prop('disabled', true);
        },
        complete: function() {
            $(element).prop('disabled', false);
        },
        success: function(json) {
            if (json['postcode_required'] == '1') {
                $('#modal-payment-address input[name=\'postcode\']').closest('.row.mb-3').addClass('required');
            } else {
                $('#modal-payment-address input[name=\'postcode\']').closest('.row.mb-3').removeClass('required');
            }

            html = '<option value="">{{ text_select }}</option>';

            if (json['zone'] && json['zone'] != '') {
                for (i = 0; i < json['zone'].length; i++) {
                    html += '<option value="' + json['zone'][i]['zone_id'] + '"';

                    if (json['zone'][i]['zone_id'] == payment_zone_id) {
                        html += ' selected="selected"';
                    }

                    html += '>' + json['zone'][i]['name'] + '</option>';
                }
            } else {
                html += '<option value="0" selected="selected">{{ text_none }}</option>';
            }

            $('#modal-payment-address select[name=\'zone_id\']').html(html);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#modal-payment-address select[name=\'country_id\']').trigger('change');

$('#modal-payment-address #button-payment-address').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call&user_token={{ user_token }}&action=payment_address&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: $('#modal-payment-address input[type=\'text\'], #modal-payment-address input[type=\'hidden\'], #modal-payment-address input[type=\'radio\']:checked, #modal-payment-address input[type=\'checkbox\']:checked, #modal-payment-address select, #modal-payment-address textarea'),
        dataType: 'html',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            console.log(json);

            $('.alert-dismissible, .invalid-tooltip').remove();
            $('.is-invalid').removeClass('is-invalid');

            if (json['error']) {
                if (json['error']['warning']) {
                    $('#modal-payment-address .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['warning'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                }

                for (i in json['error']) {
                    var element = $('#input-payment-' + i.replaceAll('_', '-'));

                    // Highlight any found errors
                    $(element).addClass('is-invalid');

                    if ($(element).parent().hasClass('input-group')) {
                        $(element).parent().after('<div class="invalid-tooltip d-inline">' + json['error'][i] + '</div>');
                    } else {
                        $(element).after('<div class="invalid-tooltip d-inline">' + json['error'][i] + '</div>');
                    }
                }
            }

            if (json['success']) {
                $('#modal-payment-address .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Shipping Address
$('#modal-shipping-address select[name=\'shipping_address\']').on('change', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=customer/customer|address&user_token={{ user_token }}&address_id=' + this.value,
        dataType: 'json',
        beforeSend: function() {
            $(element).prop('disabled', true);
        },
        complete: function() {
            $(element).prop('disabled', false);
        },
        success: function(json) {
            // Reset all fields
            $('#modal-shipping-address input[type=\'text\'], #modal-shipping-address input[type=\'text\'], #modal-shipping-address textarea').val('');
            $('#modal-shipping-address select option').not('#modal-shipping-address select[name=\'shipping_address\']').removeAttr('selected');
            $('#modal-shipping-address input[type=\'checkbox\'], #modal-shipping-address input[type=\'radio\']').removeAttr('checked');

            $('#modal-shipping-address input[name=\'firstname\']').val(json['firstname']);
            $('#modal-shipping-address input[name=\'lastname\']').val(json['lastname']);
            $('#modal-shipping-address input[name=\'company\']').val(json['company']);
            $('#modal-shipping-address input[name=\'address_1\']').val(json['address_1']);
            $('#modal-shipping-address input[name=\'address_2\']').val(json['address_2']);
            $('#modal-shipping-address input[name=\'city\']').val(json['city']);
            $('#modal-shipping-address input[name=\'postcode\']').val(json['postcode']);
            $('#modal-shipping-address select[name=\'country_id\']').val(json['country_id']);

            shipping_zone_id = json['zone_id'];

            for (i in json['custom_field']) {
                $('#modal-shipping-address select[name=\'custom_field[' + i + ']\']').val(json['custom_field'][i]);
                $('#modal-shipping-address textarea[name=\'custom_field[' + i + ']\']').val(json['custom_field'][i]);
                $('#modal-shipping-address input[name^=\'custom_field[' + i + ']\'][type=\'text\']').val(json['custom_field'][i]);
                $('#modal-shipping-address input[name^=\'custom_field[' + i + ']\'][type=\'hidden\']').val(json['custom_field'][i]);
                $('#modal-shipping-address input[name^=\'custom_field[' + i + ']\'][type=\'radio\'][value=\'' + json['custom_field'][i] + '\']').prop('checked', true);
                $('#modal-shipping-address input[name^=\'custom_field[' + i + ']\'][type=\'checkbox\'][value=\'' + json['custom_field'][i] + '\']').prop('checked', true);

                if (json['custom_field'][i] instanceof Array) {
                    for (j = 0; j < json['custom_field'][i].length; j++) {
                        $('#modal-shipping-address input[name^=\'custom_field[' + i + ']\'][type=\'checkbox\'][value=\'' + json['custom_field'][i][j] + '\']').prop('checked', true);
                    }
                }
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    }).done(function() {
        $('#modal-shipping-address select[name=\'country_id\']').trigger('change');
    });
});

var shipping_zone_id = '{{ shipping_zone_id }}';

$('#modal-shipping-address select[name=\'country_id\']').on('change', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=localisation/country|country&user_token={{ user_token }}&country_id=' + this.value,
        dataType: 'json',
        beforeSend: function() {
            $(element).prop('disabled', true);
        },
        complete: function() {
            $(element).prop('disabled', false);
        },
        success: function(json) {
            if (json['postcode_required'] == '1') {
                $('#modal-shipping-address input[name=\'postcode\']').closest('.row.mb3').addClass('required');
            } else {
                $('#modal-shipping-address input[name=\'postcode\']').closest('.row.mb3').removeClass('required');
            }

            html = '<option value="">{{ text_select }}</option>';

            if (json['zone'] && json['zone'] != '') {
                for (i = 0; i < json['zone'].length; i++) {
                    html += '<option value="' + json['zone'][i]['zone_id'] + '"';

                    if (json['zone'][i]['zone_id'] == shipping_zone_id) {
                        html += ' selected="selected"';
                    }

                    html += '>' + json['zone'][i]['name'] + '</option>';
                }
            } else {
                html += '<option value="0" selected="selected">{{ text_none }}</option>';
            }

            $('#modal-shipping-address select[name=\'zone_id\']').html(html);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#modal-shipping-address select[name=\'country_id\']').trigger('change');

$('#modal-shipping-address #button-shipping-address').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call&user_token={{ user_token }}&action=shipping_address&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: $('#modal-shipping-address input[type=\'text\'], #modal-shipping-address input[type=\'hidden\'], #modal-shipping-address input[type=\'radio\']:checked, #modal-shipping-address input[type=\'checkbox\']:checked, #modal-shipping-address select, #modal-shipping-address textarea'),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            console.log(json);

            $('.alert-dismissible, .invalid-tooltip').remove();
            $('.is-invalid').removeClass('is-invalid');

            // Check for errors
            if (json['error']) {
                if (json['error']['warning']) {
                    $('#modal-shipping-address .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['warning'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                }

                for (i in json['error']) {
                    var element = $('#modal-shipping-address #input-shipping-' + i.replaceAll('_', '-'));

                    // Highlight any found errors
                    $(element).addClass('is-invalid');

                    if ($(element).parent().hasClass('input-group')) {
                        $(element).parent().after('<div class="invalid-tooltip d-inline">' + json['error'][i] + '</div>');
                    } else {
                        $(element).after('<div class="invalid-tooltip d-inline">' + json['error'][i] + '</div>');
                    }
                }
            }

            if (json['success']) {
                $('#modal-shipping-address .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Product
$('#modal-cart input[name=\'product\']').autocomplete({
    'source': function(request, response) {
        $.ajax({
            url: 'index.php?route=catalog/product|autocomplete&user_token={{ user_token }}&filter_name=' + encodeURIComponent(request),
            dataType: 'json',
            success: function(json) {
                response($.map(json, function(item) {
                    return {
                        label: item['name'],
                        value: item['product_id'],
                        model: item['model'],
                        option: item['option'],
                        price: item['price']
                    }
                }));
            }
        });
    },
    'select': function(item) {
        $('#modal-cart input[name=\'product\']').val(item['label']);
        $('#modal-cart input[name=\'product_id\']').val(item['value']);

        if (item['option'] != '') {
            html = '<fieldset>';
            html += '  <legend>{{ entry_option }}</legend>';

            for (i = 0; i < item['option'].length; i++) {
                option = item['option'][i];

                if (option['type'] == 'select') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label for="input-option-' + option['product_option_id'] + '" class="col-form-label">' + option['name'] + '</label>';

                    html += '  <select name="option[' + option['product_option_id'] + ']" id="input-option-' + option['product_option_id'] + '" class="form-select">';
                    html += '    <option value="">{{ text_select }}</option>';

                    for (j = 0; j < option['product_option_value'].length; j++) {
                        option_value = option['product_option_value'][j];

                        html += '<option value="' + option_value['product_option_value_id'] + '">' + option_value['name'];

                        if (option_value['price']) {
                            html += ' (' + option_value['price_prefix'] + option_value['price'] + ')';
                        }

                        html += '</option>';
                    }

                    html += '  </select>';

                    html += '</div>';
                }

                if (option['type'] == 'radio') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label for="input-option-' + option['product_option_id'] + '" class="col-form-label">' + option['name'] + '</label>';

                    html += '  <select name="option[' + option['product_option_id'] + ']" id="input-option-' + option['product_option_id'] + '" class="form-select">';
                    html += '    <option value="">{{ text_select }}</option>';

                    for (j = 0; j < option['product_option_value'].length; j++) {
                        option_value = option['product_option_value'][j];

                        html += '<option value="' + option_value['product_option_value_id'] + '">' + option_value['name'];

                        if (option_value['price']) {
                            html += ' (' + option_value['price_prefix'] + option_value['price'] + ')';
                        }

                        html += '</option>';
                    }

                    html += '  </select>';
                    html += '</div>';
                }

                if (option['type'] == 'checkbox') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label class="col-form-label">' + option['name'] + '</label>';

                    html += '  <div id="input-option-' + option['product_option_id'] + '">';

                    for (j = 0; j < option['product_option_value'].length; j++) {
                        option_value = option['product_option_value'][j];

                        html += '<div class="form-check">';
                        html += '  <input type="checkbox" name="option[' + option['product_option_id'] + '][]" value="' + option_value['product_option_value_id'] + '" id="input-option-value-' + option_value['product_option_value_id'] + '" class="form-check-input"/> ' + option_value['name'];
                        html += '  <label for="input-option-value-' + option_value['product_option_value_id'] + '" class="form-check-label">';

                        if (option_value['price']) {
                            html += ' (' + option_value['price_prefix'] + option_value['price'] + ')';
                        }

                        html += '  </label>';
                        html += '</div>';
                    }

                    html += '  </div>';
                    html += '</div>';
                }

                if (option['type'] == 'image') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label for="input-option-' + option['product_option_id'] + '" class="col-form-label">' + option['name'] + '</label>';

                    html += '  <select name="option[' + option['product_option_id'] + ']" id="input-option-' + option['product_option_id'] + '" class="form-select">';
                    html += '    <option value="">{{ text_select }}</option>';

                    for (j = 0; j < option['product_option_value'].length; j++) {
                        option_value = option['product_option_value'][j];

                        html += '<option value="' + option_value['product_option_value_id'] + '">' + option_value['name'];

                        if (option_value['price']) {
                            html += ' (' + option_value['price_prefix'] + option_value['price'] + ')';
                        }

                        html += '</option>';
                    }

                    html += '  </select>';
                    ;
                    html += '</div>';
                }

                if (option['type'] == 'text') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label for="input-option-' + option['product_option_id'] + '" class="col-form-label">' + option['name'] + '</label>';
                    html += '  <input type="text" name="option[' + option['product_option_id'] + ']" value="' + option['value'] + '" placeholder="' + option['name'] + '" id="input-option-' + option['product_option_id'] + '" class="form-control"/>';
                    html += '</div>';
                }

                if (option['type'] == 'textarea') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label for="input-option-' + option['product_option_id'] + '" class="col-form-label">' + option['name'] + '</label>';
                    html += '  <textarea name="option[' + option['product_option_id'] + ']" rows="5" placeholder="' + option['name'] + '" id="input-option-' + option['product_option_id'] + '" class="form-control">' + option['value'] + '</textarea>';
                    html += '</div>';
                }

                if (option['type'] == 'file') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label class="col-form-label">' + option['name'] + '</label>';
                    html += '  <button type="button" data-oc-toggle="upload" data-oc-url="index.php?route=tool/upload|upload&user_token={{ user_token }}" data-oc-loading-text="{{ text_loading }}" class="btn btn-light"><i class="fas fa-upload"></i> {{ button_upload }}</button>';
                    html += '  <input type="hidden" name="option[' + option['product_option_id'] + ']" value="' + option['value'] + '" id="input-option-' + option['product_option_id'] + '"/></div>';
                    html += '</div>';
                }

                if (option['type'] == 'date') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label for="input-option-' + option['product_option_id'] + '" class="col-form-label">' + option['name'] + '</label>';
                    html += '  <div class="input-group date"><input type="text" name="option[' + option['product_option_id'] + ']" value="' + option['value'] + '" placeholder="' + option['name'] + '"  id="input-option-' + option['product_option_id'] + '" class="form-control"/><div class="input-group-text"><i class="fas fa-calendar"></i></div></div>';
                    html += '</div>';
                }

                if (option['type'] == 'datetime') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label for="input-option-' + option['product_option_id'] + '" class="col-form-label">' + option['name'] + '</label>';
                    html += '  <div class="input-group datetime"><input type="text" name="option[' + option['product_option_id'] + ']" value="' + option['value'] + '" placeholder="' + option['name'] + '" id="input-option-' + option['product_option_id'] + '" class="form-control"/><div class="input-group-text"><i class="fas fa-calendar"></i></div></div>';
                    html += '</div>';
                }

                if (option['type'] == 'time') {
                    html += '<div class="mb-3' + (option['required'] == 1 ? ' required' : '') + '">';
                    html += '  <label for="input-option-' + option['product_option_id'] + '" class="col-form-label">' + option['name'] + '</label>';
                    html += '  <div class="input-group time"><input type="text" name="option[' + option['product_option_id'] + ']" value="' + option['value'] + '" placeholder="' + option['name'] + '" id="input-option-' + option['product_option_id'] + '" class="form-control"/><div class="input-group-text"><i class="fas fa-calendar"></i></div></div>';
                    html += '</div>';
                }
            }

            html += '</fieldset>';

            $('#option').html(html);

            $('.date').datetimepicker({
                'format': 'YYYY-MM-DD',
                'locale': '{{ datepicker }}',
                'allowInputToggle': true
            });

            $('.time').datetimepicker({
                'format': 'HH:mm',
                'locale': '{{ datepicker }}',
                'allowInputToggle': true
            });

            $('.datetime').datetimepicker({
                'format': 'YYYY-MM-DD HH:mm',
                'locale': '{{ datepicker }}',
                'allowInputToggle': true
            });
        } else {
            $('#option').html('');
        }
    }
});


$('#modal-cart #button-product-add').on('click', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call&user_token={{ user_token }}&action=cart/add&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: $('#tab-product input[name=\'product_id\'], #tab-product input[name=\'quantity\'], #tab-product input[name^=\'option\'][type=\'text\'], #tab-product input[name^=\'option\'][type=\'hidden\'], #tab-product input[name^=\'option\'][type=\'radio\']:checked, #tab-product input[name^=\'option\'][type=\'checkbox\']:checked, #tab-product select[name^=\'option\'], #tab-product textarea[name^=\'option\']'),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible, .invalid-tooltip').remove();
            $('.is-invalid').removeClass('is-invalid');

            if (json['error']) {
                if (json['error']['warning']) {
                    $('#modal-product .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['warning'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                }

                if (json['error']['option']) {
                    for (i in json['error']['option']) {
                        var element = $('#input-option-' + i.replaceAll('_', '-'));

                        // Highlight any found errors
                        $(element).addClass('is-invalid');

                        if (element.parent().hasClass('input-group')) {
                            $(element).parent().after('<div class="invalid-tooltip d-inline">' + json['error']['option'][i] + '</div>');
                        } else {
                            $(element).after('<div class="invalid-tooltip d-inline">' + json['error']['option'][i] + '</div>');
                        }
                    }
                }

                if (json['error']['store']) {
                    $('#modal-product .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['store'] + '</div>');
                }
            } else {
                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#modal-cart #button-remove').on('click', '.btn-danger', function() {
    var element = this;

    $.ajax({
        url: 'index.php?route=sale/order|call|&user_token={{ user_token }}&action=cart/remove&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: 'key=' + encodeURIComponent(this.value),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible, .invalid-tooltip').remove();

            // Check for errors
            if (json['error']) {
                $('#tab-cart .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            } else {
                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Voucher
$('#modal-cart #button-voucher-add').on('click', function() {
    $.ajax({
        url: '{{ catalog }}index.php?route=sale/order|call&user_token={{ user_token }}&action=voucher/add&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        type: 'post',
        data: $('#tab-voucher input[type=\'text\'], #tab-voucher input[type=\'hidden\'], #tab-voucher input[type=\'radio\']:checked, #tab-voucher input[type=\'checkbox\']:checked, #tab-voucher select, #tab-voucher textarea'),
        dataType: 'json',
        beforeSend: function() {
            $('#button-voucher-add').button('loading');
        },
        complete: function() {
            $('#button-voucher-add').button('reset');
        },
        success: function(json) {
            $('.alert-dismissible, .invalid-tooltip').remove();
            $('.is-invalid').removeClass('is-invalid');

            if (json['error']) {
                if (json['error']['warning']) {
                    $('#tab-voucher').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['warning'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                }

                for (i in json['error']) {
                    var element = $('#input-' + i.replaceAll('_', '-'));

                    // Highlight any found errors
                    $(element).addClass('is-invalid');

                    $(element).after('<div class="invalid-tooltip d-inline">' + json['error'][i] + '</div>');
                }
            } else {
                $('input[name=\'from_name\']').val('');
                $('input[name=\'from_email\']').val('');
                $('input[name=\'to_name\']').val('');
                $('input[name=\'to_email\']').val('');
                $('textarea[name=\'message\']').val('');
                $('input[name=\'amount\']').val('{{ voucher_min|escape('js') }}');

                // Refresh products, vouchers and totals
                $('#button-refresh').trigger('click');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Refresh all products, vouchers and totals
$('#button-refresh').on('click', function() {
    $.ajax({
        url: 'index.php?route=sale/order|call/cart|products&user_token={{ user_token }}&store_id=' + $('select[name=\'store_id\'] option:selected').val(),
        dataType: 'json',
        beforeSend: function() {
            $('#button-refresh').button('loading');
        },
        complete: function() {
            $('#button-refresh').button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            // Check for errors
            if (json['error']) {
                if (json['error']['warning']) {
                    $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['warning'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                }

                if (json['error']['stock']) {
                    $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['stock'] + '</div>');
                }

                if (json['error']['minimum']) {
                    for (i in json['error']['minimum']) {
                        $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['minimum'][i] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                    }
                }
            }

            var shipping = false;

            html = '';

            if (json['products'].length) {
                for (i = 0; i < json['products'].length; i++) {
                    product = json['products'][i];

                    html += '<tr>';
                    html += '  <td class="text-start">' + product['name'] + ' ' + (!product['stock'] ? '<span class="text-danger">***</span>' : '') + '<br />';
                    html += '  <input type="hidden" name="product[' + i + '][product_id]" value="' + product['product_id'] + '" />';

                    if (product['option']) {
                        for (j = 0; j < product['option'].length; j++) {
                            option = product['option'][j];

                            html += '  - <small>' + option['name'] + ': ' + option['value'] + '</small><br />';

                            if (option['type'] == 'select' || option['type'] == 'radio' || option['type'] == 'image') {
                                html += '<input type="hidden" name="product[' + i + '][option][' + option['product_option_id'] + ']" value="' + option['product_option_value_id'] + '" />';
                            }

                            if (option['type'] == 'checkbox') {
                                html += '<input type="hidden" name="product[' + i + '][option][' + option['product_option_id'] + '][]" value="' + option['product_option_value_id'] + '" />';
                            }

                            if (option['type'] == 'text' || option['type'] == 'textarea' || option['type'] == 'file' || option['type'] == 'date' || option['type'] == 'datetime' || option['type'] == 'time') {
                                html += '<input type="hidden" name="product[' + i + '][option][' + option['product_option_id'] + ']" value="' + option['value'] + '" />';
                            }
                        }
                    }

                    html += '</td>';
                    html += '  <td class="text-start">' + product['model'] + '</td>';
                    html += '  <td class="text-end"><div class="input-group"><input type="text" name="product[' + i + '][quantity]" value="' + product['quantity'] + '" class="form-control"/><button type="button" data-bs-toggle="tooltip" title="{{ button_refresh }}" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-sync"></i></button></div></td>';
                    html += '  <td class="text-end">' + product['price'] + '</td>';
                    html += '  <td class="text-end">' + product['total'] + '</td>';
                    html += '  <td class="text-end" style="width: 3px;"><button type="button" value="' + product['cart_id'] + '" data-bs-toggle="tooltip" title="{{ button_remove }}" data-oc-loading-text="{{ text_loading }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button></td>';
                    html += '</tr>';

                    if (product['shipping'] != 0) {
                        shipping = true;
                    }
                }
            }

            if (!shipping) {
                $('select[name=\'shipping_method\'] option').removeAttr('selected');
                $('select[name=\'shipping_method\']').prop('disabled', true);
                $('#button-shipping-method').prop('disabled', true);
            } else {
                $('select[name=\'shipping_method\']').prop('disabled', false);
                $('#button-shipping-method').prop('disabled', false);
            }

            if (json['vouchers'].length) {
                for (i in json['vouchers']) {
                    voucher = json['vouchers'][i];

                    html += '<tr>';
                    html += '  <td class="text-start">' + voucher['description'];
                    html += '    <input type="hidden" name="voucher[' + i + '][code]" value="' + voucher['code'] + '" />';
                    html += '    <input type="hidden" name="voucher[' + i + '][description]" value="' + voucher['description'] + '" />';
                    html += '    <input type="hidden" name="voucher[' + i + '][from_name]" value="' + voucher['from_name'] + '" />';
                    html += '    <input type="hidden" name="voucher[' + i + '][from_email]" value="' + voucher['from_email'] + '" />';
                    html += '    <input type="hidden" name="voucher[' + i + '][to_name]" value="' + voucher['to_name'] + '" />';
                    html += '    <input type="hidden" name="voucher[' + i + '][to_email]" value="' + voucher['to_email'] + '" />';
                    html += '    <input type="hidden" name="voucher[' + i + '][voucher_theme_id]" value="' + voucher['voucher_theme_id'] + '" />';
                    html += '    <input type="hidden" name="voucher[' + i + '][message]" value="' + voucher['message'] + '" />';
                    html += '    <input type="hidden" name="voucher[' + i + '][amount]" value="' + voucher['amount'] + '" />';
                    html += '  </td>';
                    html += '  <td class="text-start"></td>';
                    html += '  <td class="text-end">1</td>';
                    html += '  <td class="text-end">' + voucher['price'] + '</td>';
                    html += '  <td class="text-end">' + voucher['price'] + '</td>';
                    html += '  <td class="text-end-start" style="width: 3px;"><button type="button" value="' + voucher['code'] + '" data-bs-toggle="tooltip" title="{{ button_remove }}" data-oc-loading-text="{{ text_loading }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button></td>';
                    html += '</tr>';
                }
            }

            if (!json['products'].length && !json['vouchers'].length) {
                html += '<tr>';
                html += '  <td colspan="6" class="text-center">{{ text_no_results }}</td>';
                html += '</tr>';
            }

            $('#cart').html(html);

            // Totals
            html = '';

            if (json['products'].length) {
                for (i = 0; i < json['products'].length; i++) {
                    product = json['products'][i];

                    html += '<tr>';
                    html += '  <td class="text-start">' + product['name'] + ' ' + (!product['stock'] ? '<span class="text-danger">***</span>' : '') + '<br />';

                    if (product['option']) {
                        for (j = 0; j < product['option'].length; j++) {
                            option = product['option'][j];

                            html += '  - <small>' + option['name'] + ': ' + option['value'] + '</small><br />';
                        }
                    }

                    html += '  </td>';
                    html += '  <td class="text-start">' + product['model'] + '</td>';
                    html += '  <td class="text-end">' + product['quantity'] + '</td>';
                    html += '  <td class="text-end">' + product['price'] + '</td>';
                    html += '  <td class="text-end">' + product['total'] + '</td>';
                    html += '</tr>';
                }
            }

            if (json['vouchers'].length) {
                for (i in json['vouchers']) {
                    voucher = json['vouchers'][i];

                    html += '<tr>';
                    html += '  <td class="text-start">' + voucher['description'] + '</td>';
                    html += '  <td class="text-end"></td>';
                    html += '  <td class="text-end">1</td>';
                    html += '  <td class="text-end">' + voucher['amount'] + '</td>';
                    html += '  <td class="text-end">' + voucher['amount'] + '</td>';
                    html += '</tr>';
                }
            }

            if (json['totals'].length) {
                for (i in json['totals']) {
                    total = json['totals'][i];

                    html += '<tr>';
                    html += '  <td class="text-start" colspan="4">' + total['title'] + ':</td>';
                    html += '  <td class="text-end">' + total['text'] + '</td>';
                    html += '</tr>';
                }
            }

            if (!json['totals'].length && !json['products'].length && !json['vouchers'].length) {
                html += '<tr>';
                html += '  <td colspan="5" class="text-center">{{ text_no_results }}</td>';
                html += '</tr>';
            }

            $('#total').html(html);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#history').on('click', '.pagination a', function(e) {
    e.preventDefault();

    $('#history').load(this.href);
});

$('#history').load('index.php?route=sale/order|history&user_token={{ user_token }}&order_id=' + $('input[name=\'order_id\']').val());

$('#button-history').on('click', function(e) {
    e.preventDefault();

    $.ajax({
        url: 'index.php?route=sale/order|call&user_token={{ user_token }}&action=order|history&order_id=' + $('input[name=\'order_id\']').val(),
        type: 'post',
        dataType: 'json',
        data: 'order_id=' + encodeURIComponent($('#tab-history select[name=\'order_id\']').val()) + '&notify=' + ($('input[name=\'notify\']').prop('checked') ? 1 : 0) + '&comment=' + encodeURIComponent($('#tab-history textarea[name=\'comment\']').val()),
        beforeSend: function() {
            $('#button-history').button('loading');
        },
        complete: function() {
            $('#button-history').button('reset');
        },
        success: function(json) {
            console.log(json);

            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#tab-history').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#history').load('index.php?route=sale/order|call|history&user_token={{ user_token }}&return_id=' + $('input[name=\'return_id\']').val());

                $('#tab-history').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                $('#tab-history textarea[name=\'comment\']').val('');
            }
        }
    });
});

// Checkout
$('#button-save').on('click', function() {
    var url = 'index.php?route=sale/order|call&user_token={{ user_token }}&action=order|save&store_id=' + $('select[name=\'store_id\'] option:selected').val();

    var order_id = $('input[name=\'order_id\']').val();

    if (order_id != 0) {
        url += '&order_id=' + order_id;
    }

    $.ajax({
        url: url,
        type: 'post',
        data: $('select[name=\'payment_method\'] option:selected, select[name=\'shipping_method\'] option:selected, select, textarea[name=\'comment\'], input[name=\'affiliate_id\']'),
        dataType: 'json',
        beforeSend: function() {
            $('#button-save').button('loading');
        },
        complete: function() {
            $('#button-save').button('reset');
        },
        success: function(json) {
            $('.alert-dismissible, .invalid-tooltip').remove();

            if (json['error']) {
                $('#tab-total .modal-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#tab-total .modal-body').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + '  <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['order_id']) {
                $('input[name=\'order_id\']').val(json['order_id']);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});
//--></script>
{{ footer }}